package paiement;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Calendar;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import general.NotreServlet;
import hibernate.*;
import hibernate.dao.PersonneDAO;
import hibernate.util.HibernateUtil;

public class PaiementAction extends Action{

        private static Log logger = LogFactory.getLog(LogicWebEtapesAction.class);

        public ActionForward execute( ActionMapping pMapping, ActionForm pForm, HttpServletRequest pRequest, HttpServletResponse pResponse) {
    		String command = "Z:\\Master2\\ID\\ProjetSI\\WebContent\\WEB-INF\\cgi-bin\\modulev3.exe";
    		int id=(Integer) pRequest.getSession().getAttribute("idPersonne");
    		/*String urlParameters = "param1=a&param2=b&param3=c";
    		String request = "https://preprod-tpeweb.paybox.com/cgi/MYchoix_pagepaiement.cgi";
    		URL url;				HttpURLConnection connection;
			try {
				url = new URL(request);
				connection = (HttpURLConnection) url.openConnection();
	    		connection.setDoOutput(true);
	    		connection.setDoInput(true);
	    		connection.setInstanceFollowRedirects(false);
				connection.setRequestMethod("POST");

	    		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
	    		connection.setRequestProperty("charset", "utf-8");
	    		connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
	    		connection.setUseCaches (false);
	
	    		DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
	    		wr.writeBytes(urlParameters);
	    		wr.flush();
	    		wr.close();
	    		connection.disconnect();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} */
    		PersonneDAO lDAO = new PersonneDAO();
    		try {
    			Session session = HibernateUtil.currentSession();
    			//Ouverture transaction
    			Transaction tx = session.beginTransaction();
				Personne unePersonne = lDAO.findPersonneDAO(id);
				unePersonne.setPaye(1);
				tx.commit();
				HibernateUtil.closeSession();
				
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		
                return pMapping.findForward("succes");

                } 
        
    private static final String NOM_CLASSE_COMPLET = LogicWebEtapesAction.class.getName();
    private static final String NCC = NOM_CLASSE_COMPLET;
    private static final String NOM_CLASSE = NOM_CLASSE_COMPLET.substring(NOM_CLASSE_COMPLET.lastIndexOf('.') + 1);

}
